/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.leulounge.parser;

import com.leulounge.control.Login;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JOptionPane;

/**
 *
 * @author Salaver
 */
public class TextFileParser {
    
    public String nuevalinea = System.getProperty("line.separator");
    
    public void parseBill(String text){
        try {
//			File file = new File("C:/Users/Salaver/facturera/factura.in");
			File file = new File("C:/facturera/factura.in");
 
			// if file doesnt exists, then create it
			if (!file.exists()) {
				file.createNewFile();
			}
 
			FileWriter fw = new FileWriter(file.getAbsoluteFile());
			BufferedWriter bw = new BufferedWriter(fw);
			bw.write(text);
                        bw.close();
 
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
    
    public void parseZReport(){
        try {
			String content = "[Z]";
 
//			File file = new File("C:/Users/Salaver/facturera/reporteZ.in");
			File file = new File("C:/facturera/reporteZ.in");
 
			// if file doesnt exists, then create it
			if (!file.exists()) {
				file.createNewFile();
			}
 
			FileWriter fw = new FileWriter(file.getAbsoluteFile());
			BufferedWriter bw = new BufferedWriter(fw);
			bw.write(content);
                        bw.close();
 
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
    
    public String checkZReport(){
       
//        File file = new File("C:/Users/Salaver/facturera/reporteZ.out");
        File file = new File("C:/facturera/reporteZ.out");
        BufferedReader br = null;
        String response = "";
        if (file.exists()){
		try {
			String sCurrentLine;
 
			br = new BufferedReader(new FileReader(file));
 
			while ((sCurrentLine = br.readLine()) != null) {
				response = response + sCurrentLine + nuevalinea;
			}
 
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
//                                file.delete();
                                System.out.println("RESPONSE: "+response);
				if (br != null)
					br.close();
                                if (response.contains("ERROR")){
                                    JOptionPane.showMessageDialog(Login.menu,
                                    response,
                                    "Error de Impresion",
                                    JOptionPane.ERROR_MESSAGE);
                                    return "1";
                                } else {
                                    JOptionPane.showMessageDialog(Login.menu,
                                    "Reporte Z impreso exitosamente");
                                    return "0";
                                }
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
        }
        return "2";
    }
    
    public String checkBill(){
       
//        File file = new File("C:/Users/Salaver/facturera/factura.out");
        File file = new File("C:/facturera/factura.out");
        BufferedReader br = null;
        String response = "";
        if (file.exists()){
		try {
			String sCurrentLine;
 
			br = new BufferedReader(new FileReader(file));
 
			while ((sCurrentLine = br.readLine()) != null) {
				response = response + sCurrentLine + nuevalinea;
			}
 
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
//                                file.delete();
                                System.out.println("RESPONSE: "+response);
				if (br != null)
					br.close();
                                if (response.contains("ERROR")){
                                    JOptionPane.showMessageDialog(Login.menu,
                                    response,
                                    "Error de Impresion",
                                    JOptionPane.ERROR_MESSAGE);
                                    return "1";
                                } else {
                                    JOptionPane.showMessageDialog(Login.menu,
                                    "Factura impresa exitosamente");
                                    Login.menu.resetScrolls();
                                    Login.menu.resetSpinners();
                                    return "0";
                                }
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
        }
        return "2";
    }
}



    

